package god.dao01;

/**
 * @author wang hao
 * @date 2023-02-18 10:19
 *  二分法查找某一个数
 */
public class Demo01 {


    // [1,2,3,4,5,6,7]
    //  0 1 2 3 4 5 6
    public static boolean exist(int[] sortedArr, int num){
        if (sortedArr == null || sortedArr.length == 0){
            return false;
        }

        int L = 0, R = sortedArr.length - 1;
        int mid = 0;
        while (L <= R){
            mid = L + ((R - L) >> 1);
            if (sortedArr[mid] == num){
                return true;
            }else if (sortedArr[mid] > num){
                R = mid - 1;
            }else {
                L = mid + 1;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        int[] arr = new int[]{1,2,3,3,4,5,6};
        boolean exist = Demo01.exist(arr, 6);
        System.out.println(exist);
    }
}
